package com.heartware.sh.blackhole.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.ui.activity.base.BaseActivity;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Func1;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    private static final String TAG = MainActivity.class.getSimpleName();


    private Button btn_start_one, btn_start_two, btn_start_three, btn_start_four, btn_start_five, btn_start_six,
            btn_start_seven, btn_start_eight, btn_start_nine, btn_start_ten;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getToolBarX().setTitle(R.string.app_name).setNavigationIcon(R.mipmap.ic_launcher);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        super.initView();

        btn_start_one = (Button) findViewById(R.id.btn_start_one);
        btn_start_two = (Button) findViewById(R.id.btn_start_two);
        btn_start_three = (Button) findViewById(R.id.btn_start_three);
        btn_start_four = (Button) findViewById(R.id.btn_start_four);
        btn_start_five = (Button) findViewById(R.id.btn_start_five);
        btn_start_six = (Button) findViewById(R.id.btn_start_six);
        btn_start_seven = (Button) findViewById(R.id.btn_start_seven);
        btn_start_eight = (Button) findViewById(R.id.btn_start_eight);
        btn_start_nine = (Button) findViewById(R.id.btn_start_nine);
        btn_start_ten = (Button) findViewById(R.id.btn_start_ten);

        iv = (ImageView) findViewById(R.id.iv);

        /**  Glide图片加载类, 可以加载gif图片 显示默认ic_launcher*/
        Glide.with(this).load("http://img.hb.aicdn.com/ef2eed50b290cf0451a7df0f42a379a88ec4775970ca3-6pF2x4_fw580").placeholder(R.mipmap.ic_launcher).into(iv);
        /** Gilde图片加载, 加载图片示例 显示空白*/
//        Glide.with(this).load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg").into(iv);
        /** Gilde图片加载, 加载图片示例, 显示默认ic_laucher*/
//        Glide.with(this).load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg").centerCrop().placeholder(R.mipmap.ic_launcher).crossFade().into(iv);

    }

    @Override
    public void setListener() {
        super.setListener();
        btn_start_one.setOnClickListener(this);
        btn_start_two.setOnClickListener(this);
        btn_start_three.setOnClickListener(this);
        btn_start_four.setOnClickListener(this);
        btn_start_five.setOnClickListener(this);
        btn_start_six.setOnClickListener(this);
        btn_start_seven.setOnClickListener(this);
        btn_start_eight.setOnClickListener(this);
        btn_start_nine.setOnClickListener(this);
        btn_start_ten.setOnClickListener(this);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.action_search:
                Toast.makeText(mContext, "search", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_item1:
                Toast.makeText(mContext, "item 1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_item2:
                Toast.makeText(mContext, "item 2", Toast.LENGTH_SHORT).show();
                break;
        }

        return true;
    }

    @Override
    public void onClick(View view) {

        Intent intent = null;

        switch (view.getId()){
            case R.id.btn_start_one:
                intent = new Intent(mContext, OneActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_two:
                intent = new Intent(mContext, TwoActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_three:
                intent = new Intent(mContext, ThreeActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_four:
                intent = new Intent(mContext, FourActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_five:
                intent = new Intent(mContext, FiveActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_six:
                intent = new Intent(mContext, SixActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_seven:
                intent = new Intent(mContext, SevenActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_eight:
                intent = new Intent(mContext, EightActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_nine:
//                create();
                intent = new Intent(mContext, NineActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_start_ten:
                intent = new Intent(mContext, TenActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    public void create(){

        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                Log.i(TAG, "出错了");
                subscriber.onError(new Throwable("出错了"));
            }
        }).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
            @Override
            public Observable<?> call(Observable<? extends Throwable> observable) {
                return observable.flatMap(new Func1<Throwable, Observable<Long>>() {
                    @Override
                    public Observable<Long> call(Throwable throwable) {
                        return Observable.timer(5, TimeUnit.SECONDS);
                    }
                });
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG, "onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError()");
            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG, "onNext()"+integer);
            }
        });

    }
}
